package cn.com;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import cn.com.analyze.Parser;
import cn.com.shell.Shell;
import cn.com.tool.Uitool;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;

public class Main extends Activity {
	
	Shell s;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main); 
        
        s = new Shell();
        if(!s.start())
        {
        	Uitool.alert(this, "error", "Don't have root!");
        }
        
        try {
			if(!installTCPdump())
				Uitool.alert(this, "error", "failed to install tcpdump");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			Uitool.alert(this, "error", "failed to install tcpdump");
		}
    }
    
    public void runShell(View v)
    {
        if(!s.exec("/data/data/cn.com/files/tcpdump -p -vv -s 0 -w /sdcard/capture.pcap &"))
        {
        	Uitool.alert(this, "error", "start error!");
        }
        else
        {
        	Uitool.toast(this, "start!");
        }
        
    }
    
    private ProgressDialog progressDialog;
    
    int totalBytes;
    private Handler handler = new Handler(){

		@Override
		public void handleMessage(Message msg) {
			switch(msg.what)
			{
			case 0:
				progressDialog.dismiss();
				Uitool.alert(Main.this, "", "total bytes is " + totalBytes + "bytes");
				Log.e("main", "total bytes is " + totalBytes);
				break;
			}
		}
    	
    };
    public void stop(View v)
    {
    	if(s != null)
    	{
    		if(!s.exec("killall tcpdump"))
    		{
    			Uitool.alert(this, "error", "can't kill process");
    			return;
    		}
    		if(!s.close())
    		{
    			Uitool.alert(this, "error", "can't close shell");
    			return;
    		}
    		s = null;
    		
    		progressDialog = ProgressDialog.show(this, "", "waiting...", true, false);
    		
    		new Thread(){

				@Override
				public void run() {
					// TODO Auto-generated method stub
					super.run();
					Parser p = new Parser();
					p.readPackets("/sdcard/capture.pcap");
					Main.this.totalBytes = p.getTotalBytes();
					
					handler.sendEmptyMessage(0);
				}
    			
    		}.start();
    		
    		Uitool.toast(this, "stopped");
    	}
    }
    
    private boolean installTCPdump() throws IOException
    {
    	
    	String path = this.getFilesDir().getCanonicalPath();
    	String dst = path + File.separator + "tcpdump";
    	
    	File f = new File(dst);
    	if(f.exists())
    	{
    		return true;
    	}
    	//Uitool.alert(this, "", dst);
    	
    	InputStream is = this.getResources().openRawResource(R.raw.tcpdump);
    	FileOutputStream os = new FileOutputStream(f);
    	
    	byte[] buff = new byte[2048];
    	int len;
    	while((len = is.read(buff)) != -1)
    	{
    		os.write(buff, 0, len);
    	}
    	
    	is.close();
    	os.close();
    	
    	s.exec("chmod " + 755 + " " + dst);
    	/*if(!s.exec("chmod " + 700 + " " + dst))
    	{
    		return false;
    	}*/
    	return true;
    }
}